package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.Message;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.message.MessageContentDto;
import com.sharemarking.wa.platform.mapper.MessageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class MessageService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private MessageMapper messageMapper;


    public ResponseParams<?> getMessageContent() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        String id = request.getParameter("id");

        if (StringUtils.isEmpty(id)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("id"));
        }

        MessageContentDto messageContentDto = messageMapper.getMessage(memberId, Integer.valueOf(id));
        //messageContentDto.setTitle(messageContentDto.getTitle()+messageContentDto.getBody());
        return ResponseParams.ok(messageContentDto);
    }

    public ResponseParams<?> getMessageList() throws Exception{
        Integer memberId = ss.getCurrentUserId();

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);
        List<MessageContentDto> messageContentDtos = messageMapper.getMessageList(memberId);
        messageMapper.updateState(memberId);
        return ResponseParams.ok(messageContentDtos);
    }

    public ResponseParams deleteMessageList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        messageMapper.deleteMessageList(memberId);
        return ResponseParams.ok(null);
    }
}
